﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Protocol
{
    public class ResLevelResult
    {
        public class UserInfo
        {
            public int ID;
            public string Name;
        }

        public List<UserInfo> UserInfos = new List<UserInfo>();

        public void Add(int Id, string name)
        {
            UserInfo info = new UserInfo();
            info.ID = Id;
            info.Name = name;
        }

        public byte[] Encode()
        {
            byte[] buffer;
            using (MemoryStreamExt ms = new MemoryStreamExt())
            {
                ms.WriteInt(UserInfos.Count);
                for (int i = 0; i < UserInfos.Count; i++)
                {
                    ms.WriteInt(UserInfos[i].ID);
                    ms.WriteUTF8String(UserInfos[i].Name);
                }
                buffer = new byte[ms.Length];
                buffer = ms.GetBuffer();
            }
            return buffer;
        }

        public static ResLevelResult Decode(byte[] buffer)
        {
            ResLevelResult dto = new ResLevelResult();
            using (MemoryStreamExt ms = new MemoryStreamExt(buffer))
            {
                int count = ms.ReadInt();
                dto.UserInfos = new List<UserInfo>();
                for (int i = 0; i < count; i++)
                {
                    UserInfo info = new UserInfo();
                    info.ID = ms.ReadInt();
                    info.Name = ms.ReadUTF8String();
                    dto.UserInfos.Add(info);
                }
            }
            return dto;
        }
    }
}
